package com.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.controller.base.BaseController;
import com.entity.ChanPing;
import com.entity.FuJianGuanXi;
import com.service.ChanPingService;
import com.service.FuJianGuanXiService;

@Controller
@RequestMapping("/chanping/manager")
public class ChanPingController extends BaseController<ChanPing>{
		@Autowired
		private FuJianGuanXiService fjgxService;
		@Autowired
		private ChanPingService cpService;
		
		@RequestMapping("/addcp")
		@ResponseBody
		public AjaxRes addcp(ChanPing o,String fujianId){
			System.out.println("cp"+o);
			AjaxRes res = new AjaxRes();
			try {
				cpService.insertFj(o,fujianId);
				res.setResMsg("添加成功");
			} catch (Exception e) {
				System.out.println("ER:"+e.getMessage());
				res.setResMsg("添加失败");
			}
			return res;
		}
		
		@RequestMapping("/findByPage")
		@ResponseBody
		public AjaxRes findByPage(ChanPing o,Page<ChanPing> page){
			AjaxRes res = new AjaxRes();
			System.out.println(o.getId());
			page = cpService.findByPage(o, page);
			res.setSucceed(page,"成功");
			return res;
		}
		@RequestMapping("/delcp")
		@ResponseBody
		public AjaxRes delohy(ChanPing o){
			AjaxRes res = new AjaxRes();
			cpService.delete(o);
			res.setSucceedMsg("删除成功");
			return res;
		}
		
		@RequestMapping("/delAllcp")
		@ResponseBody
		public AjaxRes delAllohy(String cks){
			AjaxRes res = new AjaxRes(); 
			String[] ck = cks.split(",");
			List<ChanPing> list = new ArrayList<ChanPing>();
			for(String s:ck){
				ChanPing o = new ChanPing();
				o.setId(s);
				list.add(o);
			}
			cpService.deleteBatch(list);
			res.setSucceedMsg("删除成功");
			return res;
		}

		
		@RequestMapping("/updatecp")
		@ResponseBody
		public AjaxRes updatecp(ChanPing o){
			AjaxRes res = new AjaxRes();
			System.out.println("产品:"+o);
			cpService.update(o);
			res.setSucceedMsg("修改成功");
			return res;
		}

		@RequestMapping("/find")
		@ResponseBody
		public AjaxRes find(ChanPing o){
			AjaxRes res = new AjaxRes();
			List<ChanPing> oList = cpService.find(o);
			res.setSucceed(oList,"获取数据成功");
			return res;
		}
		
		@RequestMapping("/findByFujian")
		@ResponseBody
		public AjaxRes findByFujian(FuJianGuanXi o){
			AjaxRes res = new AjaxRes();
			List<FuJianGuanXi> fjgxList = fjgxService.findByFujian(o);
			res.setSucceed(fjgxList,"获取数据成功");
			return res;
		}
}

